﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Configuration;
//using System.Data.SqlClient;
using System.Windows.Forms;

using ECS.Framework;
using ECS.DAL;

namespace E_ICS.E_ICS
{
    public partial class frmLocAdjust : Form
    {
        #region private properties
        //SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["con3"].ConnectionString);
        private DataSet dsLocDetail = new DataSet();
        private bool _isDirty = false;
        string chkdelete = string.Empty;
        #endregion

        #region form event
        public frmLocAdjust(DataSet _dsLocDetail)
        {
            InitializeComponent();
            dsLocDetail = _dsLocDetail;
            //SetValueScnfrmLocAdjust_Load();

        }
        private void frmLocAdjust_Shown(object sender, EventArgs e)
        {
            try
            {
                SetValueScnfrmLocAdjust_Load();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (_isDirty)
            {
                this.DialogResult = DialogResult.Yes;
            }
            this.Dispose();
        }

        //private void frmLocAdjust_Load(object sender, EventArgs e)
        //{
        //    SetValueScnfrmLocAdjust_Load();
        //}
        private void dgvLocDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int idx = 0;
                idx = e.RowIndex;
                if (e.ColumnIndex == dgvLocDetail.Columns["Column3"].Index && e.RowIndex >= 0)
                {


                    if (chkdelete.Equals("change"))
                    {
                        //Add resourse
                        //scn_LocAdjust_checkedit
                        //MessageBox.Show("Have change value do you want to save?");
                        CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_LocAdjust_checkedit"));
                     }
                    else
                    {
                        if (CommonMessageBox.ShowMessageConfirmYN(CommonResourceString.GetResourseValue("scn_Main2_com_msgbox_delete_line")) == DialogResult.Yes)
                        {
                            DeleteRow(idx);
                        }

                    }



                }
                //if (chkdelete.Equals("change"))
                //{
                //    MessageBox.Show("Have change value do you want to save?");

                //}
                //else
                //{

                //}

            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }

        }

        private void DeleteRow(int idx)
        {
            ViewEditLocationDetail.InventoryId = dsLocDetail.Tables[0].Rows[idx]["inventory_id"].ToString();
            using (Entities entity = new Entities())
            {
                entity.Open();
                DbCommand cmd = entity.CreateCommand("usp_DeleteInventoryRecord");
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(entity.CreateParameter("@in_vchApplicationID", GlobalVariable.user.ApplicationID));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchInventoryID", ViewEditLocationDetail.InventoryId));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchDevice", GlobalVariable.user.Device));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchUserID", GlobalVariable.user.UserID));
                DbDataAdapter adapter = entity.CreateDataAdapter(cmd);
                DataSet dt = new DataSet();
                adapter.Fill(dt, "dt");
                dgvLocDetail.Rows.RemoveAt(idx);
                _isDirty = true;
                chkdelete = "del";
            }
        }

        //private void DeleteRow()
        //{
        //    ViewEditLocationDetail.InventoryId = dsLocDetail.Tables[0].Rows[e.RowIndex]["inventory_id"].ToString();
        //    using (Entities entity = new Entities())
        //    {
        //        entity.Open();
        //        DbCommand cmd = entity.CreateCommand("usp_DeleteInventoryRecord");
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.Parameters.Add(entity.CreateParameter("@in_vchApplicationID", GlobalVariable.user.ApplicationID));
        //        cmd.Parameters.Add(entity.CreateParameter("@in_vchInventoryID", ViewEditLocationDetail.InventoryId));
        //        cmd.Parameters.Add(entity.CreateParameter("@in_vchDevice", GlobalVariable.user.Device));
        //        cmd.Parameters.Add(entity.CreateParameter("@in_vchUserID", GlobalVariable.user.UserID));
        //        DbDataAdapter adapter = entity.CreateDataAdapter(cmd);
        //        DataSet dt = new DataSet();
        //        adapter.Fill(dt, "dt");
        //        dgvLocDetail.Rows.RemoveAt(e.RowIndex);
        //        _isDirty = true;
        //        chkdelete = "del";
        //    }
        //}

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!chkdelete.Equals("del"))
                {
                    SaveLocationdetail();


                }

                GetPcs();  // create by por 
                this.DialogResult = DialogResult.Yes;
                this.Close();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void GetPcs()
        {
            ViewEditWHInventory.intPiece = 0;
            try
            {
                int dgvLocDetailcount = dgvLocDetail.Rows.Count;
                if (dgvLocDetailcount > 0)
                {
                    for (int i = 0; i < dgvLocDetailcount; i++)
                    {
                        ViewEditWHInventory.intPiece += Convert.ToInt32(dgvLocDetail.Rows[i].Cells[1].Value);
                    }
                }
            }
            catch (Exception ex)
            {

                CommonMessageBox.ShowMessageError(ex);
            }
        }
        #endregion

        #region private method
        private void SetValueScnfrmLocAdjust_Load()
        {
            txtSerial.Text = ViewEditWHInventory.MAWB;
            txtHAWB.Text = ViewEditWHInventory.HAWB;
            txtNo.Text = ViewEditWHInventory.Flight_No;
            txtFlight_Date.Text = ViewEditWHInventory.Flight_Date;
            txtNature_of_Good.Text = ViewEditWHInventory.Nature_of_Goods;
            showdgvLocationDetail();

        }

        private void showdgvLocationDetail()
        {
            DataTable dtLocationDetail_TB = new DataTable();
            dtLocationDetail_TB = CreateLocationDetail_TB(dsLocDetail);
            dgvLocDetail.DataSource = dtLocationDetail_TB;

        }



        private DataTable CreateLocationDetail_TB(DataSet _dtLocation)
        {
            DataTable TLocationDetail = new DataTable();
            ///////////create head column/////////////////

            TLocationDetail.Columns.Add("Location", typeof(string));
            TLocationDetail.Columns.Add("Pieces", typeof(string));
            TLocationDetail.Columns.Add("Delete");





            ///////////////////////////////////////////////
            int row = _dtLocation.Tables[0].Rows.Count;
            for (int i = 0; i < row; i++)
            {
                string location = _dtLocation.Tables[0].Rows[i]["location"].ToString().Trim();
                string pieces = _dtLocation.Tables[0].Rows[i]["piece_count"].ToString().Trim();



                TLocationDetail.Rows.Add(location, pieces);
            }
            return TLocationDetail;
        }
        private void SaveLocationdetail()
        {
            using (Entities entity = new Entities())
            {
                entity.Open();
                DbCommand cmd = entity.CreateCommand("usp_UpdateInventoryRecord");
                cmd.CommandType = CommandType.StoredProcedure;
                DbDataAdapter adapter = entity.CreateDataAdapter("");
                for (int i = 0; i < dgvLocDetail.RowCount; i++)
                {
                    ViewEditLocationDetail.Pieces_Loc = dgvLocDetail.Rows[i].Cells[1].Value.ToString();
                    ViewEditLocationDetail.Location_display = dgvLocDetail.Rows[i].Cells[0].Value.ToString();
                    ViewEditLocationDetail.InventoryId = dsLocDetail.Tables[0].Rows[i]["inventory_id"].ToString();
                    ViewEditLocationDetail.LocationID = dsLocDetail.Tables[0].Rows[i]["location_id"].ToString();

                    cmd.Parameters.Clear();
                    cmd.Parameters.Add(entity.CreateParameter("@in_vchApplicationID", GlobalVariable.user.ApplicationID));
                    cmd.Parameters.Add(entity.CreateParameter("@in_vchInventoryID", ViewEditLocationDetail.InventoryId));
                    cmd.Parameters.Add(entity.CreateParameter("@in_vchDevice", GlobalVariable.user.Device));
                    cmd.Parameters.Add(entity.CreateParameter("@in_vchUserID", GlobalVariable.user.UserID));
                    cmd.Parameters.Add(entity.CreateParameter("@in_fltPieceCount", Convert.ToDouble(ViewEditLocationDetail.Pieces_Loc)));
                    //adapter = entity.CreateDataAdapter(cmd);
                    adapter.SelectCommand = cmd;
                    DataSet dt2 = new DataSet();
                    adapter.Fill(dt2, "dt2");
                }
                this.DialogResult = DialogResult.Yes;
            }
        }
        private void dgvLocDetail_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string headerText = string.Empty;
            try
            {
                headerText = dgvLocDetail.Columns[e.ColumnIndex].HeaderText;
                if (headerText.Equals("Pieces"))    //&& !string.IsNullOrEmpty(e.FormattedValue.ToString())
                {
                    //number
                    if (!ECS.Framework.Utilities.IsNumeric(e.FormattedValue))
                    {
                        e.Cancel = true;
                        CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_LocAdjust_Msg_Check_format"));
                        //CommonMessageBox.ShowMessageError("Invalid number format");
                    }
                }

            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        #endregion

        private void frmLocAdjust_Load(object sender, EventArgs e)
        {
            SetScreen();
        }
        private void SetScreen()
        {
            this.Text = CommonResourceString.GetResourseValue("scn_LocAdjust_frm_name");
            label7.Text = CommonResourceString.GetResourseValue("scn_LocAdjust_label7");
            label8.Text = CommonResourceString.GetResourseValue("scn_LocAdjust_label8");
            lblFlightNO.Text = CommonResourceString.GetResourseValue("scn_LocAdjust_lblFlightNO");
            label3.Text = CommonResourceString.GetResourseValue("scn_LocAdjust_label3");
            label5.Text = CommonResourceString.GetResourseValue("scn_LocAdjust_label5");
            btnSave.Text = CommonResourceString.GetResourseValue("scn_LocAdjust_btnSave");
            btnCancel.Text = CommonResourceString.GetResourseValue("scn_LocAdjust_btnCancel");

        }

        private void dgvLocDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //dgvLocDetail.IsCurrentCellInEditMode(
            try
            {
                if (dgvLocDetail.Columns[1].HeaderText.Equals("Pieces"))
                {

                    chkdelete = "change";
                }
            }
            catch (Exception)
            {

                //throw;
            }


        }



        //private void SaveLocationdetail()
        //{
        //    for (int i = 0; i < dgvLocDetail.RowCount - 1; i++)
        //    {
        //        ViewEditLocationDetail.Pieces_Loc = dgvLocDetail.Rows[i].Cells[1].Value.ToString();
        //        ViewEditLocationDetail.Location_display = dgvLocDetail.Rows[i].Cells[0].Value.ToString();
        //        ViewEditLocationDetail.InventoryId = dsLocDetail.Tables[0].Rows[i]["inventory_id"].ToString();
        //        ViewEditLocationDetail.LocationID = dsLocDetail.Tables[0].Rows[i]["location_id"].ToString();
        //        //MessageBox.Show(ViewEditLocationDetail.Pieces_Loc);
        //        //MessageBox.Show(ViewEditLocationDetail.Location_display);
        //        //MessageBox.Show(ViewEditLocationDetail.InventoryId);
        //        //MessageBox.Show(ViewEditLocationDetail.LocationID);
        //        SqlCommand cmd = new SqlCommand();


        //        cmd.CommandText = "usp_UpdateInventoryRecord";
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.Connection = sqlCon;
        //        cmd.Parameters.AddWithValue("@in_vchApplicationID", ConfigurationManager.AppSettings["appID"]);
        //        cmd.Parameters.AddWithValue("@in_vchInventoryID", ViewEditLocationDetail.InventoryId);
        //        cmd.Parameters.AddWithValue("@in_vchDevice", ConfigurationManager.AppSettings["device"]);
        //        cmd.Parameters.AddWithValue("@in_vchUserID", GlobalVariable.userName);
        //        cmd.Parameters.AddWithValue("@in_fltPieceCount", Convert.ToDouble(ViewEditLocationDetail.Pieces_Loc));
        //        try
        //        {



        //            sqlCon.Open();

        //            SqlDataAdapter daInventory = new SqlDataAdapter(cmd);
        //            DataSet dt2 = new DataSet();
        //            daInventory.Fill(dt2, "dt2");
        //            sqlCon.Close();
        //            // dgvLocDetail.Rows.RemoveAt(e.RowIndex);
        //            this.DialogResult = DialogResult.Yes;
        //        }

        //        catch (Exception objEx)
        //        {
        //            string str = objEx.Message;
        //            MessageBox.Show(str);
        //            sqlCon.Close();
        //        }
        //        //                EXEC usp_UpdateInventoryRecord @in_vchApplicationID = 'EICS', @in_vchDevice = 'MY_PC', @in_vchUserID = 'ERP'
        //        //, @in_vchInventoryID = 'F975BEA4-1DAE-41FE-8FD8-00169C346185'
        //        //, @in_fltPieceCount = 900
        //    }

        //}

        //private void GetValueSaveLocdetail()
        //{

        //}


    }
}
